/*  
 *  Copyright 2006 Paul Jack.
 *
 *  This file is part of the Dex compiler suite. 
 *  
 *  Dex is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 2 of the License, or (at your
 *  option) any later version.
 *  
 *  Dex is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
 *  Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */
package dex.compiler.checker.type;


/**
 * A class or record type.
 */
final public class MemberType extends Type {
	
	
	/**
	 * The name of the type.
	 */
	final private String name;
	
	
	/**
	 * True if the type is local; false otherwise.
	 */
	final private boolean local;
	
	
	/**
	 * True if this type is a record type; false if it's a class.
	 */
	final private boolean record;


	/**
	 * Constructs a new member type occurrence.
	 * 
	 * @param local  true if the type is local, false otherwise
	 * @param name  the fully-qualified name of the user-defined type 
	 * @param def   the definition of the type
	 */
	MemberType(TypeManager m, boolean local, String name, boolean record) {
		super(m);
		if (name == null) {
			throw new IllegalArgumentException();
		}
		this.local = local;
		this.name = name;
		this.record = record;
	}
	
	
	/**
	 * Returns true if this type is a local type.
	 * 
	 * @return true if this type is a local type
	 */
	@Override
	public boolean isLocal() {
		return local;
	}
	

	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		if (local) {
			sb.append('#');
		}
		sb.append(name);
		return sb.toString();
	}
	

	/**
	 * Returns true, since member types are composed of other types.
	 * 
	 * @return true
	 */
	@Override
	public boolean isCompound() {
		return true;
	}

	
	/**
	 * Returns true if this member type is a record.
	 * 
	 * @return  true if this member type is a record
	 */
	public boolean isRecord() {
		return record;
	}


	/**
	 * Returns the name of the type.
	 */
	public String getName() {
		return name;
	}


	@Override
	public boolean canAssignTo(Type formal) {
		if (formal == this) {
			return true;
		}
		return manager.isSubclass(this, formal); 
	}
}
